<?php

class PagesController extends Controller
{
	public function actionIndex()
	{
		$model=new Articles('search');
		$model->unsetAttributes();
		if(isset($_GET['Articles']))
			$model->attributes=$_GET['Articles'];

		$this->render('index', array(
			'model'=>$model
		));
	}
    
    public function actionView($id)
    {
        $model = $this->loadModel($id);
        
        $this->render('view', array(
            'model'=>$model
        ));
    }
    
    public function actionCreate($id)
    {
        $model = $this->loadModel($id);
        
        if(isset($_POST['Articles']))
		{
			$model->attributes=$_POST['Articles'];
            $model->slug = $this->createSlug($model->title);
            $model->tables = 'helps';
            $model->table_id = 0;
            $model->status = 'A';
            $model->posteddate = date('Y-m-d H:i:s');
            $model->save();
            
            $this->redirect(array('/Dashboard/pages/view', 'id'=>$id));
        }
        
        $this->render('create', array(
            'model'=>$model
        ));
    }
    
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);
        
        if(isset($_POST['Articles']))
		{
			$model->attributes=$_POST['Articles'];
            $model->save();
            
            $this->redirect(array('/Dashboard/pages/view', 'id'=>$id));
        }
        
        $this->render('update', array(
            'model'=>$model
        ));
    }
    
    public function actionDelete($id)
	{
		$this->loadModel($id)->delete();

		// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
		if(!isset($_GET['ajax']))
			$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
	}
	
	public function loadModel($id)
	{
		$model=  Articles::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
}